package com.hui.operatelog.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hui.auth.dto.JsonResult;
import com.hui.operatelog.OperateType;
import com.hui.operatelog.entity.OperateLog;
import com.hui.operatelog.service.OperateLogService;
import com.hui.operatelog.vo.PageVo;
import com.hui.utils.ObjAnalysisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 日志管理
 * Created by LiaoYunHui on 2017/9/29/029.
 */
@Api(tags = "日志管理")
@RestController
@RequestMapping(value = "log")
public class LogController {
    @Autowired
    private OperateLogService logService;

    @ApiOperation(value = "操作日志列表")
    @GetMapping(value = "list")
    public JsonResult list(PageVo params) throws Exception {
        JsonResult<Map> jsonResult = new JsonResult<>();
        Map result = new HashMap();
        Page page = PageHelper.startPage(params.getPageNo(), params.getPageSize());
        List<OperateLog> logs = logService.list(params);
        List<Map> logList = logs.stream().map((OperateLog item) -> {
            Map<String, Object> log = null;
            Integer operateType = item.getOperateType();

            try {
                log = ObjAnalysisUtil.convertBean(item);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (null != log) {
                if (OperateType.ADD.getKey().equals(operateType)) {
                    log.put("operateType", OperateType.ADD.getValue());
                }
                if (OperateType.DELETE.getKey().equals(operateType)) {
                    log.put("operateType", OperateType.DELETE.getValue());
                }
                if (OperateType.UPDATE.getKey().equals(operateType)) {
                    log.put("operateType", OperateType.UPDATE.getValue());
                }
                if (OperateType.SELECT.getKey().equals(operateType)) {
                    log.put("operateType", OperateType.SELECT.getValue());
                }
                if (OperateType.LOGIN.getKey().equals(operateType)) {
                    log.put("operateType", OperateType.LOGIN.getValue());
                }
                if (OperateType.LOGIN_OUT.getKey().equals(operateType)) {
                    log.put("operateType", OperateType.LOGIN_OUT.getValue());
                }
            }
            return log;
        }).collect(Collectors.toList());
        result.put("count", page.getTotal());
        result.put("list", logList);
        return jsonResult.setData(result);
    }


    @ApiOperation(value = "操作日志删除")
    @GetMapping(value = "delete")
    public JsonResult delete(@RequestParam Integer logId) throws Exception {
        JsonResult<Map> jsonResult = new JsonResult<>();
        logService.delete(logId);
        return jsonResult;
    }
}
